package com.qst.web;

import com.qst.po.ClassQuestion;
import com.qst.po.JsonData;
import com.qst.po.PageData;
import com.qst.po.Qustion;
import com.qst.service.IQustionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class QuestionController {
    @Autowired
    private IQustionService qustionService;



    @RequestMapping("/toquslist")
    public String toquslist() {
        return "quslist";
    }

    @RequestMapping("getqusbypage")
    @ResponseBody
    public PageData<Qustion> getqusbypage(int page, int limit) {
        PageData<Qustion> data = new PageData<Qustion>();
        int totalcount = qustionService.getqustotalcount();
        List<Qustion> qustions = qustionService.getqusbypage(page, limit);
        data.setCode(0);
        data.setMsg("");
        data.setCount(totalcount);
        data.setData(qustions);
        return data;
    }

    @RequestMapping("toaddqus")
    public String toaddqus(){
        return "addqus";
    }
    @RequestMapping("addqus")
    @ResponseBody
    public JsonData addqus(Qustion qustion){
        JsonData data =new JsonData();
        int count =qustionService.addqus(qustion);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败，请重新添加");
        }
        return data;
    }
    @RequestMapping("toupdatequs")
    public String toupdateque(int id, HttpServletRequest request){
        Qustion qustion=qustionService.getQusbyid(id);
        request.setAttribute("qustion",qustion);
        return "updatequs";
    }
    @RequestMapping("updatequs")
    @ResponseBody
    public JsonData updatequs(Qustion qustion){
        JsonData data =new JsonData();
        int count =qustionService.updatequs(qustion);
        // System.out.println(count);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败，请重新添加");
        }
        //System.out.println(data.getCode());
        return data;
    }
    @RequestMapping("delqus")
    @ResponseBody
    public JsonData delqus(Integer id){
        JsonData data =new JsonData();
        int count =qustionService.delqus(id);

        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败，请重新添加");
        }

        return data;
    }
    @RequestMapping("toaddclassquestion")
    private String toaddclassquestion(int id,HttpServletRequest request){
        request.setAttribute("cid",id);
        return "addclassquestion";
    }
    @RequestMapping("getallqustion")
    @ResponseBody
    public List<Qustion> getallqustion(){
        List<Qustion> qustions=qustionService.getqusall();
        return qustions;
    }
    @RequestMapping("addclassquestion")
    @ResponseBody
    public JsonData addcqc(int cid,int qid){
        JsonData data =new JsonData();
        ClassQuestion classQuestion=new ClassQuestion();
        classQuestion.setCid(cid);
        classQuestion.setQid(qid);
        int count =qustionService.addClassQuestion(classQuestion);

        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败，请重新添加");
        }

        return data;
    }
    @RequestMapping("toclassquslistbycid")
    private String toclassquslist(int cid,HttpServletRequest request){
        request.setAttribute("cid",cid);
        return "classquslistbycid";
    }
    @RequestMapping("getclassqusbypagecid")
    @ResponseBody
    public PageData<Qustion> getclassqusbypagecid(int cid, int page, int limit) {
        PageData<Qustion> data = new PageData<Qustion>();
//        System.out.println(cid+' '+page+' '+limit);
        int totalcount = qustionService.getclassqustotalcount(cid);
        List<Qustion> qustions = qustionService.getclassqusbypage(cid,page,limit);
        data.setCode(0);
        data.setMsg("");
        data.setCount(totalcount);
        data.setData(qustions);
        return data;
    }
}
